﻿#include "tic_cloud.h"

using namespace json11;

/****************************************
* 函数介绍   :系统登录接口
* 输入参数   : 
* 返回值     :
*****************************************/
 int System_Login(void)
 {
   Json my_req_date = Json::object {
      //   { "dev_name", "tic_gunsu31" },
      //   { "dev_sn","tic191214gs0002"},
      //   { "dev_ip", "192.168.8.88" },
      //   { "dev_cur_latitude",  31.3866120},
      //   { "dev_cur_longitude",121.2754311},
      //  // { "poweron_time",get_poweron_time() },
      //   { "access_net_type","4G"},
    };
    
    httplib::Headers headers = {
        { "Authorization", "Basic cGlnOnBpZw==" }
     };
    
    std::string json_str = my_req_date.dump();
   
    httplib::Client cli("39.100.76.59", 9999);
    char const *param ="/auth/oauth/token?username=admin&password=123456&randomStr=70851536589300766&code=yade&grant_type=password&scope=server" ;
    auto res = cli.Post(param,headers, json_str, "json");

    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
//  std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_token=data["access_token"].string_value();

    if (rc_token[0])//token非空登录成功
    { 
        WriteToken(rc_token);
        std::cout <<"Device registration succeeded "<< std::endl; 
    }
    else 
    {   
        std::cout <<"Device registration failed "<< std::endl;
        std::cout <<"ERROR:"<<data["msg"].string_value() << std::endl;     
    }
    
      return 0;
 };

/****************************************
* 函数介绍   :设备注册接口
* 输入参数   : 
* 返回值     :
*****************************************/
 int Register_Device(void)
 {
   Json my_req_date = Json::object {
        { "dev_name", "tic_gunsu31" },
        { "dev_sn","tic191214gs0002"},
        { "dev_ip", "192.168.8.88" },
        { "dev_cur_latitude",  31.3866120},
        { "dev_cur_longitude",121.2754311},
        { "access_net_type","4G"}
    };
    
    std::string json_str = my_req_date.dump();
   
    httplib::Headers headers = {
        { "Authorization",ReadToken()  }
     };

    httplib::Client cli("39.100.76.59", 9999);
    auto res = cli.Post("/tic/resource/interfaces/registry",headers, json_str, "application/json");

    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["code"].string_value();
    std::string rc_token=data["token"].string_value(); 

    if (rc_status=="0")
    { 
      WriteToken(rc_token);
      std::cout <<"Device registration succeeded "<< std::endl; 
    }
     else 
    {   
      std::cout <<"Device registration failed "<< std::endl;
      std::cout <<"ERROR:"<<data["msg"].string_value() << std::endl;
    }
    
      return 0;
 };


 
/****************************************
* 函数介绍   :设备待执行指令获取接口
* 输入参数   : 
* 返回值     :
*****************************************/
 int Get_Action_Orders(void)
 {
   httplib::Headers headers = {
          { "Authorization", ReadToken()}
     };  
    
    httplib::Client cli("39.100.76.59", 9999);
    auto res = cli.Get("/tic/resource/interfaces/cmd_query?dev_sn=1qa2ws3ed",headers  );

    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["code"].string_value();
   
    if (rc_status=="0")
    {  
      /*待改：根据云平台实际格式解析*/ 
      std::string command_type=data["status"].string_value();
      std::cout <<"Get_Action_Orders:"<<command_type <<std::endl;
      char *p=(char*)command_type.c_str();
      Command_Execution_feedback(p[0]);    
    }
     else 
    {   
      std::cout <<"Execute instruction get failed "<< std::endl; 
      std::cout <<"ERROR:"<<data["msg"].string_value() << std::endl;
    }
    
      return 0;
 };


/****************************************
* 函数介绍   : 反馈地图查询信息
* 输入参数   : 
* 返回值     :
*****************************************/
void Feedback_map_info(void)
{  
  Json map_date= Get_Map_Info( );//获取地图信息
  Json my_req_date = Json::object 
  {
    { "token", ReadToken() },
    { "Id", map_date["id"].string_value() },
    { "name", map_date["name"].string_value() },
    { "pngName",map_date["pngName"].string_value()},
    { "createdAt", map_date["createdAt"].string_value() },
    { "update", map_date["update"].string_value()}
  };

    std::string json_str = my_req_date.dump();

    httplib::Client cli("9.100.76.59", 9999);
    auto res = cli.Post("/tic/resource/cmd_response?command_type=1", json_str, "application/json");
    
    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["code"].string_value();
    std::cout << "status="<<rc_status<< std::endl;

    if (rc_status=="0")
    {
      /* code */
    }
     else 
    {   
         /* code */
    }
    
   

};
/****************************************
* 函数介绍   : 反馈任务查询信息
* 输入参数   : 
* 返回值     :
*****************************************/
void Feedback_task_info(void)
{ 

  Json task_date= Get_Task_Info( );//获取设备反馈状态与位置信息
  Json my_req_date = Json::object 
   {
      { "token", ReadToken( )  },
      { "mapId", task_date["mapId"] },
      { "mapName",task_date["mapName"]},
      { "name",task_date["name"]},
      { "taskMap",task_date["taskMap"] },
      { "taskTotalPath",task_date["taskTotalPath"]},
      { "taskEstimateTime",task_date["taskEstimateTime"]},
      { "createdAt",task_date["createdAt"] },
      { "upDate",task_date["upDate"]}
    };

    std::string json_str = my_req_date.dump();
    httplib::Client cli("9.100.76.59", 9999);
    auto res = cli.Post("/tic/resource/cmd_response?command_type=2", json_str, "application/json");
    
    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["status"].string_value();
    std::cout << "status="<<rc_status<< std::endl;

    if (rc_status=="0")
    {
       /* code */
    }
     else 
    {   
      /* code */     
    }      

};

/****************************************
* 函数介绍   : 反馈地图与路径查询信息
* 输入参数   : 
* 返回值     :
*****************************************/
void Feedback_MapAndPath_info(void)
{  
  Json map_date= Get_Map_Info( );//获取地图信息
  Json task_date= Get_Task_Info( );//获取设备反馈状态与位置信息
  Json my_req_date = Json::object 
  {
    { "token", ReadToken() },
    { "Id", map_date["id"].string_value() },
    { "mapName", map_date["name"].string_value() },
    { "pngName",map_date["pngName"].string_value()},
    { "createdAt", map_date["createdAt"].string_value() },
    { "updateAt", map_date["updateAt"].string_value()},
    { "mapName",task_date["mapName"]},
    { "taskName",task_date["taskName"] },
    { "taskTotalPath",task_date["taskTotalPath"]},
    { "taskEstimateTime",task_date["taskEstimateTime"]},
    { "createdAt",task_date["createdAt"] },
    { "update",task_date["update"]}
  };

    std::string json_str = my_req_date.dump();
    httplib::Client cli("39.100.76.59", 9999);
    auto res = cli.Post("/tic/resource/interfaces/cmd_response?command_type=1", json_str, "application/json");

    
    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["code"].string_value();
    std::cout << "status="<<rc_status<< std::endl;

    if (rc_status=="0")
    {
      /* code */
    }
     else 
    {   
         /* code */
    }
    
   

};
/****************************************
* 函数介绍   : 反馈设备状态和位置查询信息
* 输入参数   : 
* 返回值     :
*****************************************/
void Feedback_status_info(void)
{  
  Json status_date= Get_Status_Gps_Info(  );//获取设备监控统计信息
  Json my_req_date = Json::object 
  {
      { "token", ReadToken( )  },
      { "dev_status", status_date["dev_status"] },
      { "dev_cur_latitude",status_date["dev_cur_latitude"]},
      { "dev_cur_longitude",status_date["dev_cur_longitude"]},
      { "taskTotalPath", status_date["taskTotalPath"]}
  };
    std::string json_str = my_req_date.dump();
    httplib::Client cli("39.100.76.59", 9999);
    auto res = cli.Post("/tic/resource/cmd_response?command_type=3", json_str, "application/json");
    
    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["status"].string_value();
    std::cout << "status="<<rc_status<< std::endl;

    if (rc_status=="0")
    {
       /* code */
    }
     else 
    {   
      /* code */     
    }

};



/****************************************
* 函数介绍   : 反馈设备监控统计信息获取查询信息
* 输入参数   : 
* 返回值     :
*****************************************/
void Feedback_monitors_info(void)
{    

  Json monitor_date= Get_Monitor_Info(  );//获取设备监控统计信息
  Json my_req_date = Json::object 
  {
      { "token", ReadToken( ) },
      { "Count_end_time", monitor_date["dev_status"]},
      { "work_times",monitor_date["work_times"]},
      { "work_clear_acreage", monitor_date["work_clear_acreage"] },
      { "brush_dial_status", monitor_date["brush_dial_status"]},
      { "brush_dial_speed_level", monitor_date["brush_dial_speed_level"] },
      { "spray_water_status", monitor_date["spray_water_status"]},
      { "spray_water_level", monitor_date["spray_water_level"] },
      { "induced_draft_status", monitor_date["induced_draft_status"]},
      { "induced_draft_level", monitor_date["induced_draft_level"] },
      { "water_scrabble_status",monitor_date["water_scrabble_status"]},
      { "rinsing_amount", monitor_date["rinsing_amount"]},
      { "sewage_amount", monitor_date["sewage_amount"] },
      { "battery_amount", monitor_date["battery_amount"] }
    };
    std::string json_str = my_req_date.dump();
    #ifdef DEBUG 
    httplib::Client cli("192.168.8.111", 1800);
    auto res = cli.Post("/send", json_str, "json");
    #else
    httplib::Client cli("9.100.76.59", 9999);
    auto res = cli.Post("/tic/resource/cmd_response?command_type=4", json_str, "application/json");
    #endif
    
    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["status"].string_value();
    std::cout << "status="<<rc_status<< std::endl;

    if (rc_status=="0")
    {
       /* code */
    }
     else 
    {   
      /* code */     
    }
       

};
  /****************************************
* 函数介绍   : 设备指令执行反馈接口
* 输入参数   : 
* 返回值     :
*****************************************/
 int Command_Execution_feedback(char type)
 {
   if (type=='1')
     Feedback_MapAndPath_info( );
   if (type=='3')
      Feedback_status_info( ); 
   if (type=='4')
      Feedback_status_info( ); 
     
      return 0;
 };

 /****************************************
* 函数介绍   :设备监控统计信息推送
* 输入参数   : 
* 返回值     :
*****************************************/
int Upload_Monitor_Info(void)
 {  
  
  //  Json my_req_date = Json::object {
  //     { "count_end_time", "dada" },
  //     { "work_times", 0},
  //     { "brush_dial_status", "string" },
  //     { "brush_dial_speed_level", 0 },
  //     { "spray_water_status","string" },
  //     { "spray_water_level", 0 },
  //     { "induced_draft_status","string" },
  //     { "induced_draft_level", 0},
  //     { "water_scrabble_status","asd" },
  //     { "rinsing_amount", 0 },
  //     { "sewage_amount", 0 },
  //     { "battery_amount", 0 }
  //   };  

   Json my_req_date = Json::object {   //服务器端Count_end_time C大写了
      { "Count_end_time"          , "dada" },
      { "work_times"              ,  0  } ,
      { "brush_dial_speed_level"  ,  0 },
      { "brush_dial_status"       , "string" },
      { "dev_sn"                  , "string" },
      { "induced_draft_level"     ,  0 },
      { "induced_draft_status"    , "string" },
      { "rinsing_amount"          ,  0 },
      { "sewage_amount"           ,  0 },
      { "spray_water_level"       ,  0 },
      { "spray_water_status"      , "string" },
      { "water_scrabble_status"   , "string" },
      { "work_clear_acreage"      ,  0 },
      { "battery_amount"          ,  0 }
      
  };  


    httplib::Headers headers = {
          { "Authorization", ReadToken()}
     };  


    std::string json_str = my_req_date.dump();
    httplib::Client cli("39.100.76.59", 9999);
    auto res = cli.Post("/tic/resource/interfaces/queryDeviceStatistics",headers, json_str, "application/json");
                           
    std::string err;
    Json data=Json::parse(res->body.c_str(),err);//
    std::cout << "rc_msg="<<data.dump()<< std::endl;
    std::string rc_status=data["status"].string_value();
    std::cout << "status="<<rc_status<< std::endl;

    if (rc_status=="0")
    {
      std::cout <<"Equipment monitoring statistics pushed successfully"<< std::endl; 
    }
     else 
    {   
      std::cout <<"Device monitoring statistics push failed"<< std::endl;
      std::cout <<"status:ERROR"<< std::endl;
    }
    
      return 0;
 };




